package org.zachary.agent.infrastructure.handler;

import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Set;

@Component
public class SuccessHandler extends SavedRequestAwareAuthenticationSuccessHandler {


    //该方法有两个，只需要三个参数的
    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws ServletException, IOException {
        //获取当前登陆者权限
        Set<String> roles= AuthorityUtils.authorityListToSet(authentication.getAuthorities());
        String path = "";

        if(roles.contains("ROLE_admin")){
            path="/admin.html";
        }else if(roles.contains("ROLE_user")){
            path="/common_user.html";
        } else {
            path="/home.html";
        }
        //重定向
        //response.sendRedirect(path);
    }
}
